<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="起止时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
        <el-radio-group v-model="radio" style="margin-right: 10px;" @input="changeRedio($event)">          
          <el-radio-button label="今天" />
          <el-radio-button label="整周" />
          <el-radio-button label="整月" />
          <el-radio-button label="本月" />
        </el-radio-group>
      </el-form-item>
      <el-form-item label="设备编号" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查项目" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in options"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检查单位" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in options"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="监察人" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="单据号" prop="roleName" />
      <el-table-column label="设备编号" prop="roleKey"  />
      <el-table-column label="设备名称" prop="roleSort" />
      <el-table-column label="日期" prop="roleSort" />
      <el-table-column label="下次检查时间" prop="roleSort" width="100" />
      <el-table-column label="使用单位" prop="roleSort" />
      <el-table-column label="所属资源" prop="roleSort" />
      <el-table-column label="原定检查时间" prop="roleSort" width="100" />
      <el-table-column label="实际检查时间" prop="roleSort" width="100" />
      <el-table-column label="检查单位" prop="roleSort" />
      <el-table-column label="检查人" prop="roleSort" />
      <el-table-column label="制单人" prop="roleSort" />
      <el-table-column label="检查项目" prop="roleSort" />
      <el-table-column label="检查内容" prop="roleSort" />
      <el-table-column label="检查备注" prop="roleSort" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listRole } from "@/api/system/role";
import { GetDateStr } from "@/utils/index"

export default {
  data() {
    return {
      // 时间筛选
      radio: '',
      // 下拉数据
      options: [],
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleName: undefined
      },
      // 拉黑时间
      dateRange: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 时间筛选
    changeRedio(val) {
      if (val === '今天') {
        this.dateRange = [GetDateStr(0), GetDateStr(0)]
      } else if (val === '整周') {
        this.dateRange = [GetDateStr(-7), GetDateStr(-1)]
      } else if (val === '本月') {
        const dd = new Date()
        const year = dd.getFullYear()
        const month = dd.getMonth() + 1// 获取当前月份的日期
        const monthStartDate = new Date(year, month - 1, 1)
        const monthEndDate = new Date(year, month, 1)
        const days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24)
        this.dateRange = [`${year}-${month}-01`, `${year}-${month}-${days}`]
      } else if (val === '整月') {
        // const dd = new Date()
        // const year = dd.getFullYear()
        // const month = dd.getMonth()
        // const monthStartDate = new Date(year, month - 1, 1)
        // const monthEndDate = new Date(year, month, 1)
        // const days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24)
        // this.dateRange = [`${year}-${month}-01`, `${year}-${month}-${days}`]
        this.dateRange = [GetDateStr(-30), GetDateStr(-1)]
      }
      this.getList();
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      listRole(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.list = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>
